from django.db import models

from users.models import User


class NewsCategory(models.Model):
    """
    新闻分类
    """
    name = models.CharField(max_length=10, verbose_name='名称')
    sequence = models.IntegerField(default=id, verbose_name='分类顺序')

    class Meta:
        db_table = 'tb_news_category'
        verbose_name = '新闻分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class News(models.Model):
    """新闻详情"""
    title = models.CharField(max_length=50, verbose_name='新闻标题')
    digest = models.CharField(max_length=500, verbose_name='新闻摘要')
    content = models.TextField(max_length=10000, verbose_name='新闻详情')
    image_url = models.CharField(max_length=200, default='', null=True, blank=True, verbose_name='图片地址')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    clicks = models.IntegerField(default=0, verbose_name='点击量')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')
    category = models.ForeignKey(NewsCategory, on_delete=models.PROTECT, verbose_name='从属类别')
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者')

    class Meta:
        db_table = 'tb_news'
        verbose_name = '新闻详情'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Comment(models.Model):
    """新闻评论"""
    content = models.CharField(max_length=200, default='', verbose_name='评论内容')
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='父类别')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='评论人')
    news = models.ForeignKey(News, on_delete=models.CASCADE, related_name='comment', verbose_name='所属新闻')

    class Meta:
        db_table = 'tb_comment'
        verbose_name = '新闻评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content

